
import {createStore,Store} from "vuex";
import {InjectionKey} from "vue";
import {UserInfo} from "../sql/domin";
import {post} from "../util/request";
import {login} from "../api/user";

export interface State{
    token:String
    userinfo:UserInfo | null,
    msgCount:number,
    isOnline:boolean
}

export const key: InjectionKey<Store<State>> = Symbol()

export const store = createStore<State>({
    state: {
        token: '',
        userinfo:null,
        msgCount:0,
        isOnline:false
    },
    mutations: {
        SETTOKEN(state,token){
            state.token = token
        },
        SETUSER(state,userInfo : UserInfo){
            state.userinfo = userInfo
        },
        SETMSGCOUNT(state,count:number){
            state.msgCount = count
        },
        SETONLINESTATUS(state,status:boolean){
            state.isOnline = status
        }
    },
    actions: {
        SET_TOKEN({commit},token){
            commit('SETTOKEN',token)
        },
        SETUSER({commit},userInfo : UserInfo){
            commit('SETUSER',userInfo)
        },
        LOGIN({commit},{username,password}){
            login(username,password).then(res=>{
                commit('SETTOKEN',res.data.token)
            })
        },
        LOGIN_OUT({commit}){
            commit('SETUSER',null)
			commit('SETTOKEN','')
			uni.setStorage({
				key:'token',
				data:''
			})
            uni.reLaunch({
                url:'/pages/login/index'
            })
        },
        SET_MSG_COUNT({commit},count : number){
            commit('SETMSGCOUNT',count)
        },
        SET_ONLINE_STATUS({commit},status:boolean){
           commit('SETONLINESTATUS',status)
        }
    },
    getters: {
        token:state => state.token,
        nickname:state => state.userinfo?.nickname,
        userinfo:state => state.userinfo,
        onlineStatus:state => state.isOnline,
        msgCount: state => state.msgCount,
        userId:state => state.userinfo?.id,
        deptId:state => state.userinfo?.deptId,
        deptName:state => state.userinfo?.deptName,
        role: state => state.userinfo?.role
    },
    modules: {}
});
